﻿Partial Public Class Main

    Private Sub DiskIO()
        Try
            Dim QueryDiskIO = From rows In PerflogCT.CounterDatas _
                           Where rows.CounterDetail.MachineName.Equals(machineName) _
                           And rows.CounterDetail.ObjectName.Equals("PhysicalDisk") _
                           And rows.CounterDetail.CounterName.Equals("Avg. Disk sec/Read") _
                           And Not rows.CounterDetail.InstanceName.Equals("_Total") _
                           And rows.DisplayToID.DisplayString.Equals(runName) _
                           Group rows By rows.CounterDetail.InstanceName _
                           Into lowValue = Min(rows.CounterValue), maxValue = Max(rows.CounterValue), avgValue = Average(rows.CounterValue) _
                        Select Instance = InstanceName, min = lowValue, max = maxValue, avg = avgValue


            With Me.dgIODisk
                .DataSource = QueryDiskIO.AsEnumerable
                .Columns(0).Width = 60
                .Columns(0).HeaderText = "Disk"
                .Columns(1).Width = 60
                .Columns(1).HeaderText = "Min.(ms)"
                .Columns(2).Width = 60
                .Columns(2).HeaderText = "Max.(ms)"
                .Columns(3).Width = 60
                .Columns(3).HeaderText = "Avg.(ms)"
            End With

            Dim QueryIODiskGraph = From rows In PerflogCT.CounterDatas _
                           Where rows.CounterDetail.MachineName.Equals(machineName) _
                           And rows.CounterDetail.ObjectName.Equals("PhysicalDisk") _
                           And rows.CounterDetail.CounterName.Equals("Avg. Disk sec/Read") _
                           And Not rows.CounterDetail.InstanceName.Equals("_Total") _
                           And rows.DisplayToID.DisplayString.Equals(runName) _
                                    Select rows.CounterDetail.InstanceName, rows.CounterValue, rows.CounterDateTime


            Dim DiskDrives = (From Dsk In QueryIODiskGraph
                       Select Dsk.InstanceName).Distinct

            Me.SimpleIOGraph.Series.Clear()
            For Each dsk In DiskDrives
                With Me.SimpleIOGraph
                    .Series.Add(dsk.ToString)
                    With .Series(dsk.ToString)
                        .Points.DataBind((From rows In QueryIODiskGraph
                                                                    Where rows.InstanceName.Equals(dsk)
                                                                    Select rows.CounterValue, rows.CounterDateTime).AsEnumerable, "CounterDateTime", "CounterValue", String.Empty)
                        .ChartType = DataVisualization.Charting.SeriesChartType.Line
                    End With


                End With
            Next

            Me.SimpleIOGraph.Invalidate()
        Catch ex As Exception

        End Try
    End Sub

    Private Sub dgIODisk_CellFormatting(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellFormattingEventArgs) Handles dgIODisk.CellFormatting
        If dgIODisk.Columns(e.ColumnIndex).HeaderText.Equals("Min.(ms)") Then
            If CInt(e.Value) > 20 Then
                e.CellStyle.BackColor = Color.Red
                e.CellStyle.SelectionBackColor = Color.DarkRed
            ElseIf CInt(e.Value) > 12 Then
                e.CellStyle.BackColor = Color.Orange
                e.CellStyle.SelectionBackColor = Color.SandyBrown
            Else
                e.CellStyle.BackColor = Color.LawnGreen
                e.CellStyle.SelectionBackColor = Color.Olive
            End If
        End If

        If dgIODisk.Columns(e.ColumnIndex).HeaderText.Equals("Max.(ms)") Then
            If CInt(e.Value) > 20 Then
                e.CellStyle.BackColor = Color.Red
                e.CellStyle.SelectionBackColor = Color.DarkRed
            ElseIf CInt(e.Value) > 12 Then
                e.CellStyle.BackColor = Color.Orange
                e.CellStyle.SelectionBackColor = Color.SandyBrown
            Else
                e.CellStyle.BackColor = Color.LawnGreen
                e.CellStyle.SelectionBackColor = Color.Olive
            End If
        End If

        If dgIODisk.Columns(e.ColumnIndex).HeaderText.Equals("Avg.(ms)") Then
            If CInt(e.Value) > 20 Then
                e.CellStyle.BackColor = Color.Red
                e.CellStyle.SelectionBackColor = Color.DarkRed
            ElseIf CInt(e.Value) > 12 Then
                e.CellStyle.BackColor = Color.Orange
                e.CellStyle.SelectionBackColor = Color.SandyBrown
            Else
                e.CellStyle.BackColor = Color.LawnGreen
                e.CellStyle.SelectionBackColor = Color.Olive
            End If
        End If
    End Sub
End Class
